package com.sunday.volunteer.ui.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Member;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.utils.BaseUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Pual's PC on 2016/10/14.
 */
public class SendCodeActivity extends BaseActivity {

    @Bind(R.id.title_text)
    TextView title_text;
    @Bind(R.id.mobile)
    EditText mobile;
    @Bind(R.id.code)
    EditText code;
    @Bind(R.id.send_code)
    TextView sendCode;
    private Member member;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_code);
        ButterKnife.bind(this);
        title_text.setText("填写验证码");

        if(getIntent().getIntExtra(BaseUtil.TYPE , -1) == 1){
            member = BaseApp.getInstance().getMember();
            mobile.setText( member.getMobile());
            mobile.setFocusable(false);
            mobile.setEnabled(false);
        }

    }

    @OnClick(R.id.send_code)
    void sendCode(){
        getcode();
    }

    private String mobileStr;
    private void getcode(){
        if(getIntent().getIntExtra(BaseUtil.TYPE , -1) == 1){
            mobileStr = mobile.getText().toString();
        }else {
            mobileStr = mobile.getText().toString();
        }
        Call<ResultDO> call = HttpClient.getHttpAdapter().sendCode(mobileStr);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if(response.body() == null || isFinish ){
                    return;
                }
                if(response.body().getCode() == 0){
                    new TimeCount(60000,1000,sendCode).start();
                    ToastUtils.showToast(mContext , "发送成功，注意查收短信");
                }else {
                    ToastUtils.showToast(mContext , response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {

            }
        });
    }

    @OnClick(R.id.next_btn)
    void nextBtn(){
        if(TextUtils.isEmpty(mobileStr)){
            ToastUtils.showToast(mContext , "请输入手机号");
            return;
        }
        if(TextUtils.isEmpty(code.getText().toString())){
            ToastUtils.showToast(mContext , "请输入验证码");
            return;
        }
        BaseUtil.openUpdatePasswordActivity(mContext ,mobileStr ,code.getText().toString() , getIntent().getIntExtra(BaseUtil.TYPE , -1));
    }

//    //验证验证码
//    private void validatecode(){
//        Call<ResultDO> call = HttpClient.getHttpAdapter().checkCode(member.getMobile() , code.getText().toString());
//        call.enqueue(new Callback<ResultDO>() {
//            @Override
//            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
//                if(response.body() == null || isFinish ){
//                    return;
//                }
//                if(response.body().getCode() == 0){
//
//                }else {
//                    ToastUtils.showToast(mContext , response.body().getMessage());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<ResultDO> call, Throwable t) {
//
//            }
//        });
//    }


}
